package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.CheckInDTO;
import com.xuedongzhilian.entity.CheckIn;
import com.xuedongzhilian.service.ICheckInService;
import com.xuedongzhilian.service.IRankingService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 打卡记录控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/checkin")
public class CheckInController {

    @Resource
    private ICheckInService checkInService;
    
    @Resource
    private IRankingService rankingService;

    /**
     * 打卡功能
     */
    @PostMapping
    public Result checkIn(@RequestBody CheckInDTO checkInDTO) {
        // 检查今天是否已经打过卡
        Long userId = UserHolder.getUser().getId();
        Integer type = checkInDTO.getType();
        LocalDate today = LocalDate.now();
        
        CheckIn existingCheckIn = checkInService.lambdaQuery()
                .eq(CheckIn::getUserId, userId)
                .eq(CheckIn::getType, type)
                .eq(CheckIn::getCheckInDate, today)
                .one();
                
        if (existingCheckIn != null) {
            return Result.fail("今天已经打过卡了");
        }
        
        CheckIn checkIn = new CheckIn();
        BeanUtils.copyProperties(checkInDTO, checkIn);
        checkIn.setUserId(userId);
        checkIn.setCheckInDate(today);
        checkIn.setCreateTime(LocalDateTime.now());
        checkIn.setUpdateTime(LocalDateTime.now());
        
        boolean success = checkInService.save(checkIn);
        if (success) {
            return Result.ok(checkIn.getId());
        } else {
            return Result.fail("打卡失败");
        }
    }

    /**
     * 获取用户打卡记录
     */
    @GetMapping
    public Result getUserCheckIns(@RequestParam(required = false) Integer type) {
        Long userId = UserHolder.getUser().getId();
        List<CheckIn> checkIns;
        
        if (type != null) {
            checkIns = checkInService.lambdaQuery()
                    .eq(CheckIn::getUserId, userId)
                    .eq(CheckIn::getType, type)
                    .orderByDesc(CheckIn::getCheckInDate)
                    .list();
        } else {
            checkIns = checkInService.lambdaQuery()
                    .eq(CheckIn::getUserId, userId)
                    .orderByDesc(CheckIn::getCheckInDate)
                    .list();
        }
        
        return Result.ok(checkIns);
    }

    /**
     * 获取打卡记录详情
     */
    @GetMapping("/{id}")
    public Result getCheckIn(@PathVariable Long id) {
        CheckIn checkIn = checkInService.getById(id);
        if (checkIn == null) {
            return Result.fail("打卡记录不存在");
        }
        return Result.ok(checkIn);
    }

    /**
     * 删除打卡记录
     */
    @DeleteMapping("/{id}")
    public Result deleteCheckIn(@PathVariable Long id) {
        boolean success = checkInService.removeById(id);
        if (success) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    /**
     * 获取连续打卡天数
     */
    @GetMapping("/streak")
    public Result getCheckInStreak(@RequestParam Integer type) {
        Long userId = UserHolder.getUser().getId();
        return checkInService.getCheckInStreak(userId, type);
    }

    /**
     * 获取打卡排行榜
     */
    @GetMapping("/ranking")
    public Result getCheckInRanking(@RequestParam Integer type, 
                                   @RequestParam(defaultValue = "7") Integer days) {
        return rankingService.getCheckInRanking(type, days);
    }
}